package com.lestar.www.wuliancamtest.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.lestar.www.wuliancamtest.R;
import com.lestar.www.wuliancamtest.WulianCam.Device;
import com.lestar.www.wuliancamtest.WulianCam.WulianCamManager;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

/**
 * User: EEE
 * Date: 2016/8/2
 * Time: 17:48
 */
public class DeviceListActivity extends AppCompatActivity {
    private static final String TAG = "DeviceListActivity";
    private ListView mDeviceListView;
    private ArrayAdapter<String> mAdapter;
    private List<Device> mDeviceList;

    public static final String DEVICE_KEY = "device";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);

        Button addDeviceBtn = (Button) findViewById(R.id.add_device_btn);
        assert addDeviceBtn != null;
        addDeviceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: try: getAllDeviceInformationByMulticast

                Log.d(TAG, "on btn Click: ");
                Intent intent = new Intent(DeviceListActivity.this, WifiSettingActivity.class);
                startActivity(intent);
            }
        });
        mDeviceListView = (ListView) findViewById(R.id.device_list_lv);
        mDeviceListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "onItemClick: position:" + position + "\t id:" + id);
                Device device = mDeviceList.get(position);
                Intent intent = new Intent(DeviceListActivity.this, DeviceDetailActivity.class);
                intent.putExtra(DEVICE_KEY, device);
                startActivity(intent);
            }
        });
        List<String> dummyData = new ArrayList<>();
        mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dummyData);

        mDeviceListView.setAdapter(mAdapter);
        WulianCamManager.getInstance(this).getDeviceList()
                .subscribe(new Action1<List<Device>>() {
                    @Override
                    public void call(List<Device> devices) {
                        mDeviceList = devices;
                        mAdapter.addAll(getDeviceNameList());
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    private List<String> getDeviceNameList() {
        List<String> rets = new ArrayList<>();
        for (Device device: mDeviceList) {
            rets.add(device.getDid());
        }
        return rets;
    }

}
